package com.zhuhjay.app.web.controller.system;

import com.zhuhjay.common.base.controller.BaseController;
import com.zhuhjay.common.web.model.vo.PageInfoVo;
import com.zhuhjay.service.system.model.dto.NoticeDto;
import com.zhuhjay.service.system.model.vo.NoticeInfoVo;
import com.zhuhjay.service.system.service.SystemNoticeReadService;
import com.zhuhjay.service.system.service.SystemNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhuhjay
 * @since 2023-04-13
 */
@Api(tags = "系统通知")
@Validated
@RestController
@RequestMapping("/notice")
public class SystemNoticeController extends BaseController {

    @Resource
    private SystemNoticeService noticeService;

    @Resource
    private SystemNoticeReadService noticeReadService;

    @ApiOperation("获取通知列表")
    @GetMapping
    public List<NoticeInfoVo> list() {
        return noticeService.noticesList();
    }

    @ApiOperation("用户已读通知")
    @PutMapping("read/{id}")
    public void read(@PathVariable Integer id) {
        noticeReadService.read(id);
    }

    // ------------------------------ 管理员权限 ------------------------------

    @ApiOperation("分页查询通知列表")
    @PreAuthorize("@perm.hasPerm('system:manager:notice:list')")
    @GetMapping("page/{pageNum}/{pageSize}")
    public PageInfoVo<NoticeInfoVo> page(
            @PathVariable @Range(min = 1, max = Integer.MAX_VALUE) Integer pageNum,
            @PathVariable @Range(min = 5, max = 30) Integer pageSize) {
        return noticeService.noticesPage(pageNum, pageSize);
    }

    @ApiOperation("新增通知")
    @PreAuthorize("@perm.hasPerm('system:manager:notice:add')")
    @PostMapping
    public void add(@RequestBody @Valid NoticeDto notice) {
        noticeService.add(notice);
    }

    @ApiOperation("修改通知")
    @PreAuthorize("@perm.hasPerm('system:manager:notice:edit')")
    @PutMapping("{id}")
    public void update(@PathVariable Integer id, @RequestBody @Valid NoticeDto notice) {
        noticeService.updateNotice(id, notice);
    }

    @ApiOperation("修改通知状态")
    @PreAuthorize("@perm.hasPerm('system:manager:notice:edit')")
    @PutMapping("status/{id}")
    public void updateStatus(@PathVariable Integer id) {
        noticeService.updateNoticeStatus(id);
    }

    @ApiOperation("删除通知")
    @PreAuthorize("@perm.hasPerm('system:manager:notice:delete')")
    @DeleteMapping("{id}")
    public void delete(@PathVariable Integer id) {
        noticeService.deleteNotice(id);
    }

}
